"""
Case Type   : cm_agent参数
Case Name   : 使用gs_guc set -Z cmagent -c方式修改参数incremental_build值为全量重建备节点
Create At   : 2025/4/3
Owner       : ningyali
Description :
    1.查询参数incremental_build默认值
    2.修改参数值为no
    3.使用reload方式使其生效
    4.修改参数值为off
    5.使用reload方式使其生效
    6.修改参数值为false
    7.使用reload方式使其生效
    8.修改参数值为0
    9.使用reload方式使其生效
    10.恢复参数默认值
    11.使用reload方式使其生效
Expect      :
    1.执行成功
    2.执行成功
    3.执行成功
    4.执行成功
    5.执行成功
    6.执行成功
    7.执行成功
    8.执行成功
    9.执行成功
    10.执行成功
    11.执行成功
History     :
"""

import os
import unittest

from testcase.utils.ComInstall import CmInstall
from testcase.utils.ComInstall import CmUnInstall
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class CMServer(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.root_nodes = (Node('PrimaryRoot'), Node('Standby1Root'),
                           Node('Standby2Root'))
        self.user_nodes = (Node('PrimaryDbUser'), Node('Standby1DbUser'),
                           Node('Standby2DbUser'))
        self.common = Common()
        self.log_path = os.path.join(
            os.path.dirname(macro.DB_INSTANCE_PATH), 'gaussdb_log',
            self.user_nodes[0].ssh_user, 'cm', 'cm_agent')

        text = '----prestep1:安装cm;expect:执行成功----'
        self.log.info(text)
        self.install_pkg_path = os.path.dirname(macro.DB_SCRIPT_PATH)
        self.cm = CmInstall(self.install_pkg_path, self.pri_sh.node.ssh_user)
        res = self.cm.update_xml_for_cm(*self.root_nodes)
        self.assertGreater(res.count('cmDir'), 1, f'执行失败: {text}')

        res = self.cm.cm_install(self.user_nodes[0])
        self.assertIn('Install CM tool success', res, f'执行失败: {text}')

    def test_trans(self):
        text = '----step1:查询参数incremental_build默认值;expect:执行成功----'
        self.log.info(text)
        q_logdir_cmd = f"source {macro.DB_ENV_PATH};" \
                       f"cm_ctl list --param --agent | grep incremental_build"
        q_res = self.common.get_sh_result(self.user_nodes[0], q_logdir_cmd)
        self.assertIn('incremental_build = on', q_res, f'执行失败: {text}')

        text = '----step2:修改参数值为no;expect:执行成功----'
        self.log.info(text)
        set_cmd = f"source {macro.DB_ENV_PATH};" \
                  f"gs_guc set -Z cmagent -c incremental_build=\"no\""
        set_res = self.common.get_sh_result(self.user_nodes[0], set_cmd)
        self.assertIn(
            'ALL: Success to perform gs_guc!', set_res, f'执行失败: {text}')

        text = '----step3:使用reload方式使其生效;expect:执行成功----'
        self.log.info(text)
        reload_cmd = f'source {macro.DB_ENV_PATH};' \
                     f'cm_ctl reload --param --agent'
        reload_res = self.common.get_sh_result(self.user_nodes[0], reload_cmd)
        self.assertIn(
            'cm_ctl: reload cm_agent.conf success', reload_res, f'执行失败: {text}')

        q_res = self.common.get_sh_result(self.user_nodes[0], q_logdir_cmd)
        self.assertIn('incremental_build = no', q_res, f'执行失败: {text}')

        text = '----step4:修改参数值为off;expect:执行成功----'
        self.log.info(text)
        set_cmd = f"source {macro.DB_ENV_PATH};" \
                  f"gs_guc set -Z cmagent -c incremental_build=\"off\""
        set_res = self.common.get_sh_result(self.user_nodes[0], set_cmd)
        self.assertIn(
            'ALL: Success to perform gs_guc!', set_res, f'执行失败: {text}')

        text = '----step5:使用reload方式使其生效;expect:执行成功----'
        self.log.info(text)
        reload_cmd = f'source {macro.DB_ENV_PATH};' \
                     f'cm_ctl reload --param --agent'
        reload_res = self.common.get_sh_result(self.user_nodes[0], reload_cmd)
        self.assertIn(
            'cm_ctl: reload cm_agent.conf success', reload_res, f'执行失败: {text}')

        q_res = self.common.get_sh_result(self.user_nodes[0], q_logdir_cmd)
        self.assertIn('incremental_build = off', q_res, f'执行失败: {text}')

        text = '----step6:修改参数值为false;expect:执行成功----'
        self.log.info(text)
        set_cmd = f"source {macro.DB_ENV_PATH};" \
                  f"gs_guc set -Z cmagent -c incremental_build=\"false\""
        set_res = self.common.get_sh_result(self.user_nodes[0], set_cmd)
        self.assertIn(
            'ALL: Success to perform gs_guc!', set_res, f'执行失败: {text}')

        text = '----step7:使用reload方式使其生效;expect:执行成功----'
        self.log.info(text)
        reload_cmd = f'source {macro.DB_ENV_PATH};' \
                     f'cm_ctl reload --param --agent'
        reload_res = self.common.get_sh_result(self.user_nodes[0], reload_cmd)
        self.assertIn(
            'cm_ctl: reload cm_agent.conf success', reload_res, f'执行失败: {text}')

        q_res = self.common.get_sh_result(self.user_nodes[0], q_logdir_cmd)
        self.assertIn('incremental_build = false', q_res, f'执行失败: {text}')

        text = '----step8:修改参数值为0;expect:执行成功----'
        self.log.info(text)
        set_cmd = f"source {macro.DB_ENV_PATH};" \
                  f"gs_guc set -Z cmagent -c incremental_build=\"0\""
        set_res = self.common.get_sh_result(self.user_nodes[0], set_cmd)
        self.assertIn(
            'ALL: Success to perform gs_guc!', set_res, f'执行失败: {text}')

        text = '----step9:使用reload方式使其生效;expect:执行成功----'
        self.log.info(text)
        reload_cmd = f'source {macro.DB_ENV_PATH};' \
                     f'cm_ctl reload --param --agent'
        reload_res = self.common.get_sh_result(self.user_nodes[0], reload_cmd)
        self.assertIn(
            'cm_ctl: reload cm_agent.conf success', reload_res, f'执行失败: {text}')

        q_res = self.common.get_sh_result(self.user_nodes[0], q_logdir_cmd)
        self.assertIn('incremental_build = 0', q_res, f'执行失败: {text}')

        text = '----step10:恢复参数默认值;expect:执行成功----'
        self.log.info(text)
        set_cmd = f"source {macro.DB_ENV_PATH};" \
                  f"gs_guc set -Z cmagent -c incremental_build=\"on\""
        set_res = self.common.get_sh_result(self.user_nodes[0], set_cmd)
        self.assertIn(
            'ALL: Success to perform gs_guc!', set_res, f'执行失败: {text}')

        text = '----step11:使用reload方式使其生效;expect:执行成功----'
        self.log.info(text)
        reload_cmd = f'source {macro.DB_ENV_PATH};' \
                     f'cm_ctl reload --param --agent'
        reload_res = self.common.get_sh_result(self.user_nodes[0], reload_cmd)
        self.assertIn(
            'cm_ctl: reload cm_agent.conf success', reload_res, f'执行失败: {text}')

    def tearDown(self):
        text = '----poststep1:卸载cm;expect:执行成功----'
        self.log.info(text)
        self.un_cm = CmUnInstall(self.install_pkg_path,
                                 self.pri_sh.node.ssh_user)
        res = self.un_cm.cm_uninstall(
            self.user_nodes[0], '--deleteData --deleteBinary')
        self.assertIn('Uninstall CM tool success', res, f'执行失败: {text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
